<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>掷骰子</title>
</head>
<body>
<script>
    //骰子
    const touzi = ["皇冠", "锚", "红桃", "黑桃", "梅花", "方块"];
    // 返回随机数
    function rand(m,n) {
        return m + Math.floor((n - m + 1) * Math.random());
    }
    //返回骰子的任意一面
    function randFace() {
        let index = rand(0,5);
        return touzi[index];
    }
    //下注
    let funds = 50;//本金
    let round = 0;//回合次数
    while (funds > 0 && funds < 100) {
        let bets = {
            皇冠 : 0,
            锚 : 0,
            红桃 : 0,
            黑桃 : 0,
            梅花 : 0,
            方块 : 0
        };
        round ++;
        console.log(`第${round}回合`)
        console.log(`\t 本金或剩余金钱：${funds}p`);
        let totalBet = rand(1,funds);
        // 特殊情况
        if (totalBet === 7){
            totalBet = funds;
            bets.红桃 = totalBet;
        }else {
            //随机下注
            let remaining = totalBet;
            do {
                let bet = rand(1, remaining);
                let face = randFace();
                bets[face] = bets[face] + bet;
                remaining = remaining - bet;
            } while (remaining > 0);
        }
        funds -= totalBet;
        console.log(`\t下注详情：` + Object.keys(bets).map(face => `${face}:${bets[face]}￥`).join(",") + `(总共下注：${totalBet}￥)`);
    const hand = [];
    for(let roll = 0;roll < 3;roll ++){
        hand.push(randFace());
    }
    console.log(`\t开奖：${hand.join(',')}`);
    //收集赢到的钱
    let winnings = 0;
    for(let die = 0;die < hand.length; die ++){
        let face = hand[die];
        if(bets[face] > 0){
            winnings += bets[face];
        }
    }
        funds += winnings;
        console.log(`\t赢:${winnings}`);
    }


</script>
</body>
</html>